package com.ouheng.common.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
public class KuaidiRestfull {
	private static String restfullurl="http://poll.kuaidi100.com/poll/query.do";//本地restfull
	
	public static String common(Map<String,Object> map1){
		
		
		String param =JSON.toJSONString(map1);
		
		
		
		String customer ="EBA5FF4CE22FA8F3610492EC6BF96AC6";
		String keys = "HpXkcbTf4827";
		String sign = MD5.encode(param+keys+customer);
		
		
		
         Map<String, Object> params = new HashMap();
		params.put("param",param);
		params.put("sign",sign);
		params.put("customer",customer);
		
	        CloseableHttpClient httpclient = HttpClients.createDefault();  
	         String RESTFUL_BASE_URL=restfullurl;
	        HttpPost httppost = new HttpPost(RESTFUL_BASE_URL); 
	        // 创建参数队列    
	        List<NameValuePair> formparams = new ArrayList<NameValuePair>(); 
	        Set<String> set = params.keySet();
	        Iterator<String> it = set.iterator();
	        while(it.hasNext())
	        {
	            String key = it.next();
	            formparams.add(new BasicNameValuePair(key, (String) params.get(key)));
	        }
	        
	        UrlEncodedFormEntity uefEntity = null;
	        try {  
	            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");  
	            httppost.setEntity(uefEntity);  
	            CloseableHttpResponse response = httpclient.execute(httppost);  
	            try {
	                HttpEntity entity = response.getEntity();  
	                if (entity != null) {  
	                    String jsonStr = EntityUtils.toString(entity, "UTF-8");
	                    return jsonStr;
	                }  
	            } finally {  
	                response.close();  
	            }  
	        } catch (ClientProtocolException e) {  
	            e.printStackTrace();  
	        } catch (UnsupportedEncodingException e1) {  
	            e1.printStackTrace();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }catch(Exception e){
	        	e.printStackTrace();
	        } finally {  
	            // 关闭连接,释放资源    
	            try {  
	                httpclient.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	        return null;
	  }
	  
	  
	
	

}
